package com.linln.modules.system.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.linln.common.utils.UUIDGenerator;
import com.linln.modules.system.dao.BeautyProductMapper;
import com.linln.modules.system.dao.CustomMapper;
import com.linln.modules.system.dao.ProductPicsMapper;
import com.linln.modules.system.dto.ProdSubDto;
import com.linln.modules.system.dto.ProductDto;
import com.linln.modules.system.entity.BeautyProduct;
import com.linln.modules.system.entity.ProdSub;
import com.linln.modules.system.entity.ProductPics;
import com.linln.modules.system.entity.ProductPicsExample;
import com.linln.modules.system.vo.admins.AdminProductVo;
import com.linln.modules.system.vo.admins.ProductDetailVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class ProductServices {

    @Autowired
    private BeautyProductMapper productMapper;

    @Autowired
    private ProductPicsMapper productPicsMapper;

    @Autowired
    private CustomMapper customMapper;

    public Page<AdminProductVo> getIndexPager(Integer pageNum, Integer pageSize, String name) {
        PageHelper.startPage(pageNum, pageSize, true);
        Page<AdminProductVo> pager = productMapper.getAllProdPager(name);
        long total = pager.getTotal();
        if (total > 0) {
            List<AdminProductVo> result = pager.getResult();
            result.forEach(prod -> {
                int states = prod.getStates();
                int kinds = prod.getKinds();
                if (kinds == 0) {
                    prod.setStrKinds("医疗服务");
                } else if(kinds == 1) {
                    prod.setStrKinds("普通商品");
                }else if(kinds == 2){
                  Map<String,String> map =  productMapper.getSubInfo(prod.getId());
                  prod.setSubPoints(map.get("subPoints"));
                  prod.setSubPrices(map.get("subPrices"));
                    prod.setStrKinds("爱帽饰品");
                }
                if (states == 0) {
                    prod.setStrStates("上架中");
                } else if (states == 1) {
                    prod.setStrStates("已下架");
                }
            });
        }
        return pager;
    }

    @Transactional
    public int saveProduct(ProductDto dto, ProdSubDto sdto) {
        BeautyProduct product = new BeautyProduct();
        String prodId = UUIDGenerator.generate();
        Date now = new Date();
        product.setId(prodId).setCompanyId(dto.getCompanyId())
                .setDeleted(0).setStates(0).setProdName(dto.getProdName())
                .setThumb(dto.getThumb()).setImages(dto.getImages())
                .setDescription(dto.getDescription()).setOldPrice(dto.getOldPrice())
                .setNumbers(dto.getNumbers()).setPoints(dto.getPoints())
                .setKinds(dto.getKinds()).setProfiles(dto.getProfiles())
                .setCreateTime(now).setMinipic(dto.getMinipic());
        String[] set = dto.getImagesSet();
        List<ProductPics> picsList = new ArrayList<>();
        for (int i = 0; i < set.length; i++) {
            ProductPics pics = new ProductPics();
            pics.setCreateTime(now);
            pics.setDeleted(0);
            pics.setId(UUIDGenerator.generate());
            pics.setProdId(prodId);
            pics.setNums(i + 1);
            pics.setUpdateTime(now);
            pics.setUrls(set[i]);
            pics.setPos(0);
            picsList.add(pics);
        }

        int insert = productMapper.insert(product);
        customMapper.batchInsertProductPic(picsList);
        if(sdto.getSubNames().length>0){
            List<ProdSub> picsList1 = new ArrayList<>();

            for(int i=0;i<sdto.getSubNames().length;i++){
                ProdSub pics = new ProdSub();
                pics.setSubname(sdto.getSubNames()[i]);
                pics.setPrice(sdto.getSubPrice()[i]);
                pics.setPicUrl(sdto.getSubminipic()[i]);
                pics.setSorts(sdto.getSubSorts()[i]);
                pics.setSubPoints(sdto.getSubPoints()[i]);
                pics.setId(UUIDGenerator.generate());
                pics.setProdId(prodId);
                pics.setCreateTime(now);
                pics.setUpdateTime(now);
                pics.setStates(0);
                picsList1.add(pics);
            }
            customMapper.insertProdSub(picsList1);
        }

        return insert;
    }

    @Transactional
    public int updateProduct(ProductDto dto,ProdSubDto sdto) {
        String prodId = dto.getId();
        customMapper.batchUpdateProductPics(prodId);
        Date now = new Date();
        if(sdto.getSubNames().length>0){
           int count =  customMapper.updateOldSub(prodId);

               List<ProdSub> picsList1 = new ArrayList<>();

               for(int i=0;i<sdto.getSubNames().length;i++){
                   ProdSub pics = new ProdSub();
                   pics.setSubname(sdto.getSubNames()[i]);
                   pics.setPrice(sdto.getSubPrice()[i]);
                   pics.setPicUrl(sdto.getSubminipic()[i]);
                   pics.setSorts(sdto.getSubSorts()[i]);
                   pics.setSubPoints(sdto.getSubPoints()[i]);
                   pics.setId(UUIDGenerator.generate());
                   pics.setProdId(prodId);
                   pics.setCreateTime(now);
                   pics.setUpdateTime(now);
                   pics.setStates(0);
                   picsList1.add(pics);
               }
               customMapper.insertProdSub(picsList1);

        }
        BeautyProduct product = new BeautyProduct();
        product.setId(prodId).setCompanyId(dto.getCompanyId())
                .setProdName(dto.getProdName())
                .setThumb(dto.getThumb()).setImages(dto.getImages())
                .setDescription(dto.getDescription()).setOldPrice(dto.getOldPrice())
                .setNumbers(dto.getNumbers()).setPoints(dto.getPoints())
                .setKinds(dto.getKinds()).setProfiles(dto.getProfiles())
                .setUpdateTime(new Date()).setMinipic(dto.getMinipic());
        String[] set = dto.getImagesSet();

        List<ProductPics> picsList = new ArrayList<>();
        for (int i = 0; i < set.length; i++) {
            ProductPics pics = new ProductPics();
            pics.setCreateTime(now);
            pics.setDeleted(0);
            pics.setProdId(prodId);
            pics.setId(UUIDGenerator.generate());
            pics.setNums(i + 1);
            pics.setUpdateTime(now);
            pics.setPos(0);
            pics.setUrls(set[i]);
            picsList.add(pics);
        }
        int insert = productMapper.updateByPrimaryKeySelective(product);
        customMapper.batchInsertProductPic(picsList);
        return insert;
    }

    public ProductDetailVo getProductDetailVoById(String id) {
        ProductDetailVo detail = customMapper.getProductDetailById(id);
        return detail;
    }
    public List<ProdSub> getProdSubs(String id){
        List<ProdSub> list =customMapper.getProdSubs(id);
        return list;
    }

    public int lockAndRelease(String id, int state) {
        BeautyProduct product = new BeautyProduct();
        product.setId(id).setStates(state).setUpdateTime(new Date());
        int end = productMapper.updateByPrimaryKeySelective(product);
        return end;
    }


    public int deleteProduct(String id, int state) {
        BeautyProduct product = new BeautyProduct();
        product.setId(id).setDeleted(state).setUpdateTime(new Date());
        int end = productMapper.updateByPrimaryKeySelective(product);
        return end;
    }

    public BeautyProduct getBeautyProductById(String id) {
        BeautyProduct product = productMapper.selectByPrimaryKey(id);
        return product;
    }

    public int saveProductOrder(String id, int sorts) {
        BeautyProduct product = new BeautyProduct();
        product.setId(id).setSorts(sorts).setUpdateTime(new Date());
        int end = productMapper.updateByPrimaryKeySelective(product);
        return end;
    }

    public List<String> picUrls(String prodId){

        ProductPicsExample example = new ProductPicsExample();
        example.createCriteria().andProdIdEqualTo(prodId).andDeletedEqualTo(0);
        example.setOrderByClause("nums asc,create_time desc");
        List<ProductPics> pics = productPicsMapper.selectByExample(example);
        if (!pics.isEmpty()){
            List<String> collect = pics.stream().map(productPics -> productPics.getUrls()).collect(Collectors.toList());
            return collect;
        }
        return null;
    }

    public List<ProductPics> getPicUrls(String prodId){
        ProductPicsExample example = new ProductPicsExample();
        example.createCriteria().andProdIdEqualTo(prodId).andDeletedEqualTo(0);
        example.setOrderByClause("nums asc,create_time desc");
        List<ProductPics> pics = productPicsMapper.selectByExample(example);
        return pics;
    }

    public ProdSub getProdSub(String subId) {
        ProdSub ps =   customMapper.getProdSub(subId);
        return  ps;
    }
}
